package zt.song.security;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author 宋伟宁
 * @Date 2023/11/18
 * @Version 1.0
 **/
@Component
public class MyAuthFaile implements AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
           response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
           response.setCharacterEncoding("utf-8");
           response.setContentType("application/json");
           //response.setHeader("Content-Type", "application/json; charset=utf-8");
        PrintWriter out = response.getWriter();
        out.write("{\"status\":\"error\",\"msg\":\"登录失败\" }");
        out.flush();
        out.close();
    }
}
